package cn.cyl.sys.controller;

import cn.cyl.basic.util.AjaxResult;
import cn.cyl.basic.util.PageList;
import cn.cyl.sys.domain.DictionaryDetail;
import cn.cyl.sys.query.DictionaryDetailQuery;
import cn.cyl.sys.service.DictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门接口类",description = "一些CRUD操作")
@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {

    @Autowired
    private DictionaryDetailService dictionaryDetailService;

    /**
     * 接口：查所有
     * @return
     */
    @ApiOperation(value = "接口：查所有")
    @GetMapping
    public List<DictionaryDetail> findAll(){
       return dictionaryDetailService.loadAll();
    }

    /**
     * 接口：查一个
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：查一个")
    @GetMapping("/{types_id}")
    public DictionaryDetail findOne(@PathVariable("types_id") Long id){
        return dictionaryDetailService.loadOne(id);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param dictionaryDetail
     * @return
     */
    @ApiOperation(value = "接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId()==null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：高级和分页
     * @param dictionaryDetailQuery
     * @return
     */
    @ApiOperation(value = "接口：高级和分页")
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.queryPage(dictionaryDetailQuery);

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除操作失败");
        }
    }




}
